<?php
include_once (dirname(__FILE__).'/../lib/adodb/adodb.inc.php');
class Lxdb
{
	private $ispdo=false;
	private $db;
	
	public function __construct($dbconn='')
	 	{
	 		if(!empty($dbconn)){$this->db=$dbconn;}else{
				$this->db=include(dirname(__FILE__).'/../conf/db.config.php');
			}
			if($this->db["pdo"] && class_exists('PDO')){$this->ispdo=true;}
	 	}
	
/*通用数据库操作定义*/
	//主库操作
	private function dbconn()
    {
		if($this->ispdo){
			$conn = new PDO("mysql:host={$this->db['host']};dbname={$this->db['name']};charset=utf8",$this->db["user"],$this->db["pass"],array(PDO::ATTR_PERSISTENT=>true));
		}else{
			$conn = ADONewConnection("mysqli");
			$conn->PConnect($this->db["host"], $this->db["user"], $this->db["pass"], $this->db["name"]);
		}
    	return $conn;
    }
	//分库操作
	private function dbconn_r(){
		if($this->ispdo){
			$conn = new PDO("mysql:host={$this->db['host_r']}:{$this->db['port']};dbname={$this->db['name']}",$this->db["user"],$this->db["pass"],array(PDO::ATTR_PERSISTENT=>true));	
		}else{
			$conn = ADONewConnection("mysqli");
			$conn->PConnect($this->db["host_r"], $this->db["user"], $this->db["pass"], $this->db["name"]);
		}
    	return $conn;
	}
	
	//读库
	private function dblink($sql,$mode='')
	{
		if($mode=='r' && !empty($this->db["host_r"])){
			$this->dbr = $this->dbconn_r();	
		}else{
			$this->dbr=$this->dbconn();
		}
		if($this->ispdo){
			if($mode=='r'){
				$ret = $this->dbr->query($sql);	
			}else{
				$ret = $this->dbr->exec($sql);
			}
		}else{
			$this->dbr->Execute("set names 'utf8'");
			$ret = $this->dbr->Execute($sql) or die($this->dbr->ErrorMsg());
		}
		return $ret;
	}
	#增加一条记录
	public function setdata($table,$into)
	{
		if(!empty($this->db["dbbefore"])) $table = $this->db["dbbefore"].$table;
		if(is_array($into) && count($into)>1){
			foreach($into as $k=>$v){$addinfo .= $k."=";if(is_numeric($v)){$addinfo .= $v.",";}else{$addinfo .= "'".$v."',";}}
			$addinfo = trim($addinfo,",");
		}else{
			$addinfo = $into;
		}
		$sql = "INSERT INTO {$table} SET {$addinfo}";
     //   var_dump($sql);exit;
		$ret = $this->dblink($sql);
		if($this->ispdo){
			$newid=$this->dbr->lastInsertId();
		}else{
			$newid=$this->dbr->insert_id();
		}
		return $newid;
	}
	
	#读某条数据
	public function viewdata($select="*",$table,$where="")
	{
		if(!empty($this->db["dbbefore"])) $table = $this->db["dbbefore"].$table;
		if(is_array($where) && count($where)>0){
			foreach($where as $k=>$v){$whereinfo .= $k."=";if(is_numeric($v)){$whereinfo .= $v." AND ";}else{$whereinfo .= "'".$v."' AND ";}}
			$whereinfo = rtrim($whereinfo," AND ");
		}else{
			$whereinfo = $where;
		}
		$sql = "SELECT {$select} FROM {$table} {$whereinfo}";
		$ret = $this->dblink($sql,'r');
		if(!empty($ret))
		{
			if($this->ispdo){$rs = $ret->fetch();}else{$rs = $ret->FetchRow();}
		}else{
			$rs="";
		}
		return $rs;
	}
	
	#读一个特定值
	public function oncedata($select,$table,$where="")
	{
		if(!empty($this->db["dbbefore"])) $table = $this->db["dbbefore"].$table;
		if(is_array($where) && count($where)>0){
			foreach($where as $k=>$v){$whereinfo .= $k."=";if(is_numeric($v)){$whereinfo .= $v." AND ";}else{$whereinfo .= "'".$v."' AND ";}}
			$whereinfo = rtrim($whereinfo," AND ");
		}else{
			$whereinfo = $where;
		}
		$sql= "SELECT {$select} FROM {$table} {$whereinfo}";
		$ret = $this->dblink($sql,'r');
		if(!empty($ret))
		{
			if($this->ispdo){$row = $ret->fetch();}else{$row = $ret->FetchRow();}
			if(is_array($row)){$rs=$row[$select];}else{$rs=$row;}
		}else{
			$rs="";	
		}
		return $rs;
	}
	
	#读一组数据
	public function arrdata($select,$table,$where="",$sort="",$limit="")
	{
		$rs = array();
		if(!empty($sort)){$sort =" ORDER BY {$sort}";}
		if(!empty($this->db["dbbefore"])) $table = $this->db["dbbefore"].$table;
		if(is_array($where) && count($where)>0)
		{
			foreach($where as $k=>$v)
			{
				$whereinfo .= $k."=";
				if(is_numeric($v))
				{
					$whereinfo .= $v." AND ";
				}
				else
				{
					$whereinfo .= "'".$v."' AND ";
				}
			}
				$whereinfo = rtrim($whereinfo," AND ");
		}else{
			$whereinfo = $where;
		}
		$sql = "SELECT {$select} FROM {$table} {$whereinfo} {$sort} {$limit}";
		$ret = $this->dblink($sql,'r');
		if($this->ispdo){
			$rs = $ret->fetchALL();
		}else{
			if(!empty($ret))
			{
				while($row = $ret->FetchRow()){array_push($rs,$row);}
			}
		}
		return $rs;
	}
	
	#统计数据
	public function numdata($table,$where="")
	{
		if(!empty($this->db["dbbefore"])) $table = $this->db["dbbefore"].$table;
		if(is_array($where) && count($where)>0){
			foreach($where as $k=>$v){$whereinfo .= $k."=";if(is_numeric($v)){$whereinfo .= $v." AND ";}else{$whereinfo .= "'".$v."' AND ";}}
			$whereinfo = rtrim($whereinfo," AND ");
		}else{
			$whereinfo = $where;
		}
		$sql = "select count(*) as num from {$table} {$whereinfo}";	
		$ret = $this->dblink($sql,'r');
		if(!empty($ret))
		{
			if($this->ispdo){$rs = $ret->fetch();}else{$rs = $ret->FetchRow();}
			$num = $rs["num"];
		}else{
		$num=0;
		}
		return $num;
	}
	
	#更新某条数据
	public function updata($table,$vole,$where="")
	{
		if(!empty($this->db["dbbefore"])) $table = $this->db["dbbefore"].$table;
		if(is_array($where) && count($where)>0){
			foreach($where as $k=>$v){$whereinfo .= $k."=";if(is_numeric($v)){$whereinfo .= $v." AND ";}else{$whereinfo .= "'".$v."' AND ";}}
			$whereinfo = rtrim($whereinfo," AND ");
		}else{
			$whereinfo = $where;
		}
		if(is_array($vole) && count($vole)>0){
			foreach($vole as $k=>$v){$intoinfo .= $k."=";if(is_numeric($v)){$intoinfo .= $v.",";}else{$intoinfo .= "'".$v."',";}}
			$intoinfo = rtrim($intoinfo,",");
		}else{
			$intoinfo = $vole;
		}
		$sql = "update {$table} set {$intoinfo} {$whereinfo}";

		$ret = $this->dblink($sql);
		return $ret;
	}
	
	#删除某条数据
	public function deldata($table,$where="")
	{
		if(!empty($this->db["dbbefore"])) $table = $this->db["dbbefore"].$table;
		if(is_array($where) && count($where)>0){
			foreach($where as $k=>$v){$whereinfo .= $k."=";if(is_numeric($v)){$whereinfo .= $v." AND ";}else{$whereinfo .= "'".$v."' AND ";}}
			$whereinfo = rtrim($whereinfo," AND ");
		}else{
			$whereinfo = $where;
		}
		$sql = "DELETE FROM {$table} {$where} ";
		$ret = $this->dblink($sql);
		return $ret;
	}
	
	
	//事务处理 $sqlarr=array(SQL,SQL,SQL);
	public function gowork($sqlarr){
		$conn = $this->dbconn();
		$fate=0;
		if($this->ispdo){
			try{
				$conn->beginTransaction();
				foreach($sqlarr as $k=>$v){
					$ret[$k] = $conn->exec($v);
				}
				foreach($ret as $k=>$v){
					if(empty($ret[$k])){$fate=1;}	
				}
				if($fate){ throw new PDOException('事务处理失败，已回滚操作');}
				$conn->commit();
				return true;
			}catch(PDOException $e){
				$conn->rollBack();
				return false;
			}
		}else{
			$conn->BeginTrans();
			foreach($sqlarr as $k=>$v){
				
				$ret[$k] = $conn->Execute($v);
			}
			foreach($ret as $k=>$v){
				if(empty($ret[$k])){$fate=1;}
			}
			if($fate){
				$conn->RollbackTrans();	
				return false;
			}else{
				$conn->CommitTrans();
				return true;
			}
		}
		return false;
	}
	
/*数据库操作结束*/
}
